/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$(document).ready(function () {
    updateSessionTimeOutCallBack();
    setInterval(function () {
        $.ajax({
            url: "model/com.swiss.mooh.controller/VerifySessionTimeOutCallBack.php",
            type: "POST",
            success: function (data, textStatus, jqXHR) {
                if (data == "TIMEOUT") {
                    var r = confirm("Session expire (30 mins), Please login again!");
                    if (r == true) {
                        window.location.href = "index.php";
                    } else {
                        window.location.href = "index.php";
                    }
                }
            }
        });
    }, 10000);
    $("#logout_click").click(function () {
        $.ajax({
            url: "model/com.swiss.mooh.controller/LogoutDesSession.php",
            type: 'POST',
            success: function (data, textStatus, jqXHR) {
                alert(data);
                window.location.assign("index.php");
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("ไม่สามารถติดต่อกับ Server ได้");
            }
        });
    });
});
function addingMenuLevelOne(dialog, menuName) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 0, 'menuName': menuName},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            $("#main_menu_list").html(data);
            dialog.dialog("close");
            $("#menu_name").val("");
            setTimeout($.unblockUI, 100);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

}
function addingMenuLevelTwo(dialog, menuName, ref_id) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 3, 'menuName': menuName, 'ref_id': ref_id},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            $("#menu_level_one").html(data);
            dialog.dialog("close");
            $("#menu_name1").val("");
            setTimeout($.unblockUI, 100);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });

}
function deleteMainMenu(menuID) {
    updateSessionTimeOutCallBack();
    if (confirm("Are you sure?")) {
        $.ajax({
            url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
            type: 'POST',
            data: {'type': 1, 'menuID': menuID},
            beforeSend: function (xhr) {
                $.blockUI({css: {
                        border: 'none',
                        padding: '15px',
                        backgroundColor: '#fff',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }, message: '<img src="assets/images/gears.gif"/>'});
            },
            success: function (data, textStatus, jqXHR) {
                $("#main_menu_list").html(data);
                setTimeout($.unblockUI, 100);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    } else {
        e.preventDefault();
    }
}
function editMainMenu(menuID, menuName) {
    updateSessionTimeOutCallBack();
    state = "Edit";
    var dialog = $("#dialog-form").dialog({
        autoOpen: false,
        height: 200,
        width: 350,
        modal: true
    });
    dialog.dialog("open");
    $("#menu_name").val(menuName);
    $("#mainMenuID").val(menuID);
}
function updateMainMenu(menuID, newMenuname) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 2, 'menuID': menuID, 'newMenuname': newMenuname},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            $("#main_menu_list").html(data);
            $("#menu_name").val("");
            setTimeout($.unblockUI, 100);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}
function editMenuLevel3(menuID, menuName, refID) {
    updateSessionTimeOutCallBack();
    stateLevelTwo = "Edit";
    var dialog = $("#dialog-form_levelTwo").dialog({
        autoOpen: false,
        height: 200,
        width: 350,
        modal: true
    });
    dialog.dialog("open");
    $("#menu_name2").val(menuName);
    $("#mainMenuIDLevelTwo").val(menuID);
    $("#refMenuIDLevelTwo").val(refID);
}
function updateMainMenuLevelOne(menuID, newMenuname, refID) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 5, 'menuID': menuID, 'ref_id': refID, 'menuName': newMenuname},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            if (data == "" || data == null) {
                $("#menu_level_one").html("No data found");
                $("#menu_name1").val("");
                setTimeout($.unblockUI, 100);
            } else {
                $("#menu_level_one").html(data);
                $("#menu_name1").val("");
                setTimeout($.unblockUI, 100);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}
function getMenuLevelOne(mainMenuID) {
    updateSessionTimeOutCallBack();
    $("#add_menu_level2").removeClass("hide_add_menu");
    $("#add_menu_level3").addClass("hide_add_menu");
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 999, 'mainMenuID': mainMenuID},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            if (data == "" || data == null) {
                $("#menu_level_one").html("No data found");
                $("#mainMenuID").val(mainMenuID);
                $("#menu_level_two").html("Please select menu level 1..");
                setTimeout($.unblockUI, 100);
            } else {
                $("#menu_level_one").html(data);
                $("#mainMenuID").val(mainMenuID);
                $("#menu_level_two").html("Please select menu level 1..");
                setTimeout($.unblockUI, 100);
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}
function deleteMenuLevel1(menuID, ref_id) {
    updateSessionTimeOutCallBack();
    if (confirm("Are you sure?")) {
        $.ajax({
            url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
            type: 'POST',
            data: {'type': 4, 'menuID': menuID, 'ref_id': ref_id},
            beforeSend: function (xhr) {
                $.blockUI({css: {
                        border: 'none',
                        padding: '15px',
                        backgroundColor: '#fff',
                        '-webkit-border-radius': '10px',
                        '-moz-border-radius': '10px',
                        opacity: .5,
                        color: '#fff'
                    }, message: '<img src="assets/images/gears.gif"/>'});
            },
            success: function (data, textStatus, jqXHR) {
                if (data == "" || data == null) {
                    $("#menu_level_one").html("No data found");
                    $("#menu_name1").val("");
                    setTimeout($.unblockUI, 100);
                } else {
                    $("#menu_level_one").html(data);
                    $("#menu_name1").val("");
                    setTimeout($.unblockUI, 100);
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    } else {
        e.preventDefault();
    }
}
function editMenuLevel1(menuID, menuName, refID) {
    updateSessionTimeOutCallBack();
    stateLevelOne = "Edit";
    var dialog = $("#dialog-form_levelOne").dialog({
        autoOpen: false,
        height: 200,
        width: 350,
        modal: true
    });
    dialog.dialog("open");
    $("#mainMenuIDLevelOne").val(menuID);
    $("#menu_name1").val(menuName);
    $("#refMenuIDLevelOne").val(refID);

}
function getMenuLevelTwo(levelOneMenuID) {
    updateSessionTimeOutCallBack();
    $("#add_menu_level3").removeClass("hide_add_menu");
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 9999, 'mainMenuID': levelOneMenuID},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            if (data == "" || data == null) {
                $("#menu_level_two").html("No data found");
                $("#refMenuIDLevelTwo").val(levelOneMenuID);
                setTimeout($.unblockUI, 100);
            } else {
                $("#menu_level_two").html(data);
                $("#refMenuIDLevelTwo").val(levelOneMenuID);
                setTimeout($.unblockUI, 100);
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}
function addMenuLevelThree(dialog, menuName, ref_id) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 6, 'menuName': menuName, 'ref_id': ref_id},
        success: function (data, textStatus, jqXHR) {
            $("#menu_level_two").html(data);
            dialog.dialog("close");
            $("#menu_name2").val("");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}
function deleteMenuLevel3(menuID, ref_id) {
    updateSessionTimeOutCallBack();
    if (confirm("Are you sure?")) {
        $.ajax({
            url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
            type: 'POST',
            data: {'type': 7, 'menuID': menuID, 'ref_id': ref_id},
            success: function (data, textStatus, jqXHR) {
                if (data == "" || data == null) {
                    $("#menu_level_two").html("No data found");
                    $("#menu_name2").val("");
                } else {
                    $("#menu_level_two").html(data);
                    $("#menu_name2").val("");
                }

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(errorThrown);
            }
        });
    } else {
        e.preventDefault();
    }
}
function updateMenuLevelTwo(menuID, menuName, refID) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: 'model/com.swiss.mooh.controller/CreateMenuController.php',
        type: 'POST',
        data: {'type': 8, 'menuID': menuID, 'ref_id': refID, 'menuName': menuName},
        success: function (data, textStatus, jqXHR) {
            if (data == "" || data == null) {
                $("#menu_level_two").html("No data found");
                $("#menu_name2").val("");
            } else {
                $("#menu_level_two").html(data);
                $("#menu_name2").val("");
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(errorThrown);
        }
    });
}
function showContentTable(menuID, menuName) {
    updateSessionTimeOutCallBack();
    $("#menuIDSelect").val(menuID);
    $("#content_manage").show("fast");
    $.ajax({
        url: "model/com.swiss.mooh.controller/ContentDescriptionController.php",
        type: 'POST',
        data: {'type': 99, 'menuID': menuID},
        success: function (data, textStatus, jqXHR) {
            $("#load_desc_content").html(data);
        }
    });
}
function deleteDesriptionByID(descID, menuID) {
    updateSessionTimeOutCallBack();
    $.ajax({
        url: "model/com.swiss.mooh.controller/DeleteDescriptionByID.php",
        type: 'POST',
        data: {'descID': descID},
        beforeSend: function (xhr) {
            $.blockUI({css: {
                    border: 'none',
                    padding: '15px',
                    backgroundColor: '#fff',
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .5,
                    color: '#fff'
                }, message: '<img src="assets/images/gears.gif"/>'});
        },
        success: function (data, textStatus, jqXHR) {
            if (data == 200) {
                $.post("model/com.swiss.mooh.controller/DeleteFileByDescID.php", {'descID': descID}, function (deleteFileResult) {
                    if (deleteFileResult == 200) {
                        $.post("model/com.swiss.mooh.controller/DeleteImageByDescID.php", {'descID': descID}, function (deleteImageResult) {
                            if (deleteImageResult == 200) {
                                $.ajax({
                                    url: "model/com.swiss.mooh.controller/ContentDescriptionController.php",
                                    type: 'POST',
                                    data: {'type': 99, 'menuID': menuID},
                                    success: function (data, textStatus, jqXHR) {
                                        $("#load_desc_content").html(data);
                                    }
                                });
                                setTimeout($.unblockUI, 100);
                            } else {
                                alert(deleteImageResult);
                                setTimeout($.unblockUI, 100);
                            }
                        });
                    } else {
                        alert(deleteFileResult);
                        setTimeout($.unblockUI, 100);
                    }
                });
            } else {
                alert(data);
                setTimeout($.unblockUI, 100);
            }

        }
    });
}
function getDescriptionByID(descID, menuID) {    
    window.location = "main_edit_description_page.php?descID=" + descID + "&menuID=" + menuID;
}
function updateSessionTimeOutCallBack() {
    $.ajax({
        url: "model/com.swiss.mooh.controller/UpdateSessionTimeOutCallBack.php",
        type: "POST",
        success: function (data, textStatus, jqXHR) {
            return textStatus;
        },
        error: function (jqXHR, textStatus, errorThrown) {
            return textStatus;
        }
    });
}